package com.world.cat.service.back;

import com.world.cat.dao.back.CategoryDao;
import com.world.cat.enums.CategoryType;
import com.world.cat.model.back.Category;
import com.world.cat.service.sys.BaseService;
import com.world.common.pojo.Result;
import com.world.common.pojo.tree.ZTreeNode;
import com.world.common.util.CollectionUtil;
import com.world.common.util.SysUtil;
import com.world.core.dao.GenericRepository;
import com.world.core.dao.Restrictions;
import com.world.core.dao.SimpleExpression;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
@Transactional
public class CategoryService extends BaseService<Category> {
    @Resource
    private CategoryDao categoryDao;

    @Override
    public GenericRepository getDao() {
        return categoryDao;
    }

    public Category saveCategory(Category category) {
        if (SysUtil.isEmpty(category.getId())) {
            Category categorySaved = categoryDao.save(category);
            return categorySaved;
        } else {
            Category categoryDb = categoryDao.getOne(category.getId());
            categoryDb.setName(category.getName());
            categoryDb.setSeq(category.getSeq());
            categoryDao.save(categoryDb);
            return categoryDb;
        }
    }

    public ZTreeNode getCategoryTree(CategoryType categoryType) {
        Set<String> hasResourceIds = new HashSet<>();
        List<Category> categorys = categoryDao.findByCategoryType(categoryType);
        Map<String, List<Category>> categorysParentMap = new HashMap<>();
        if (CollectionUtil.isNotEmpty(categorys)) {
            for (Category category : categorys) {
                List<Category> categorysChild = categorysParentMap.get(category.getParentId());
                if (categorysChild == null) {
                    categorysChild = new ArrayList<>();
                    categorysParentMap.put(category.getParentId(), categorysChild);
                }
                categorysChild.add(category);
            }
        }
        ZTreeNode rootNode = new ZTreeNode();
        rootNode.setId("0");
        rootNode.setName("根");
        rootNode.setOpen(true);
        return getTreeNode(rootNode, categorysParentMap, hasResourceIds);
    }

    private static ZTreeNode getTreeNode(ZTreeNode node, Map<String, List<Category>> resourcesParentMap, Set<String> hasResourceIds) {
        List<Category> childMenus = resourcesParentMap.get(node.getId());
        if (CollectionUtil.isNotEmpty(childMenus)) {
            for (Category menu : childMenus) {
                ZTreeNode treeNode = new ZTreeNode();
                if (hasResourceIds.contains(menu.getId())) {
                    treeNode.setChecked(true);
                } else {
                    treeNode.setChecked(false);
                }
                treeNode.setName(menu.getName());
                treeNode.setId(menu.getId());
                treeNode.setpId(menu.getParentId());

                node.getChildren().add(getTreeNode(treeNode, resourcesParentMap, hasResourceIds));
            }
        }
        return node;
    }

    //转移分类
    public void trans(String sourceId, String targetId) {
        Category category = categoryDao.getOne(sourceId);
        if (category != null) {
            category.setParentId(targetId);
            categoryDao.save(category);
        }
    }

    public Result delCategory(String[] ids) {
        List<SimpleExpression> expressionListCheck = new ArrayList<>();
        expressionListCheck.add(Restrictions.in("parentId", CollectionUtil.arrayToSet(ids), true));
        List<Category> categoryParent = categoryDao.findAll(expressionListCheck);
        if (CollectionUtil.isNotEmpty(categoryParent)) {
            return new Result(false, "删除失败！子类别不为空");
        }
        List<SimpleExpression> expressionList = new ArrayList<>();
        expressionList.add(Restrictions.in("id", CollectionUtil.arrayToSet(ids), true));
        List<Category> os = getDao().findAll(expressionList);
        getDao().delete(os);
        return new Result(true, "删除成功");
    }
}
